<template>
	<view id="domxee-content" :class="globalData.defaultThemes">
		<view id="header_wrapper">
			<nii-navbar :globalData="globalData" :scroll="scroll" back :background="globalData.colors.current.section">
				<view slot="title">
					<view id="title" class="fw-bolder">{{renders.main_title}}</view>
				</view>
			</nii-navbar>
		</view>
		<view id="content_wrapper">
			<!-- <view id="card">
				<view class="card min-padding">
					<view class="card_box section" :style="styles.card">
						<view class="titles d-flex justify-content-between align-items-center">
							<view class="title fw-bold">{{renders.title}}</view>
							<view class="safe d-flex align-items-center" v-if="renders.safe.enable">
								<view class="img_box">
									<u--image :src="renders.safe.img" width="40rpx" height="40rpx" mode="aspectFit" :bgColor="globalData.colors.current.section" class="img">
									  <template v-slot:loading>
										 <image src="@/static/media/images/loading.png" mode="aspectFit"></image>
									  </template>
									  <template v-slot:error>
										 <image src="@/static/media/images/loading.png" mode="aspectFit"></image>
									  </template>
									</u--image>
								</view>
								<view class="text fw-bolder">{{renders.safe.text}}</view>
							</view>
						</view>
						<view class="amount fw-bolder">{{(renders.amount/100).toFixed(2)}}</view>
						<view class="tip fw-bold">{{renders.tip}}</view>
					</view>
				</view>
			</view> -->
			<view id="lists" v-if="renders.lists.enable">
				<view class="lists min-padding">
					<view class="lists_box">
						<view class="title fw-bolder">{{renders.lists.title}}</view>
						<view class="contents">
							<view class="content d-flex justify-content-between align-items-center" v-for="(content, index) in data.sections.lists.lists" :key="index">
								<view class="left d-flex align-items-center">
									<view class="img_box overflow-hidden">
										<u--image :src="content.icon" width="60rpx" height="60rpx" mode="aspectFit" :bgColor="globalData.colors.current.section" class="img">
										  <template v-slot:loading>
											 <image src="@/static/media/images/loading.png" mode="aspectFit"></image>
										  </template>
										  <template v-slot:error>
											 <image src="@/static/media/images/loading.png" mode="aspectFit"></image>
										  </template>
										</u--image>
									</view>
									<view class="infos">
										<view class="content fw-bolder">{{content.content}}</view>
										<view class="time less_text">{{content.create_time}}</view>
									</view>
								</view>
								<view class="right">
									<view class="amount fw-bolder">{{content.amount_format}}</view>
									<!-- <view class="tax color" v-if="content.tax.length > 0">{{content.tax}}</view> -->
								</view>
							</view>
							<view class="lists_loading" v-if="data.sections.lists.mode == 'loading'">
								<u-loading-icon mode="circle" :inactive-color="globalData.colors.theme.color" :textColor="globalData.colors.theme.color" :color="globalData.colors.current.vice" size="13" textSize="13" text="正在加载列表..."></u-loading-icon>
							</view>
							<view class="lists_finished text-center fw-bold less_text" v-if="data.sections.lists.mode == 'finished'">— 已到底部 —</view>
							<view class="lists_empty text-center" v-if="data.sections.lists.mode == 'empty'">
								<view class="domxee_icon less_text iconfont icon-search"></view>
								<view class="text less_text fw-bold">暂无相关{{renders.lists.title}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				globalData: {windowWidth: 0, statusBarHeight: 0, navHeight: 0, navigationBarHeight: 0, wechatCapsule: {width: 0, height: 0, top: 0, right: 0, bottom: 0, left: 0}, defaultThemes: [], colors: {}, configs: {} },
				scroll: 0,
				current_page: {route: '', params: {}, url: ''},
				ready: false,
				options: {

				},
				styles: {
					card: ''
				},
				shows: {

				},
				renders: {
					background: '',
					title: '',
					amount: 0,
					tip: '',
					safe: {enable: false, img: '', text: ''},
					lists: {enable: false, title: '', formData: {}}
				},
				data: {
					sections: {
						formData: {type: '', page: 1, alias: ''},
						lists: {mode: 'loading', lists: []},
					},
				},
				request_url: ''
			}
		},
		onReady() {						//页面渲染完毕后操作（动态获取/调整元素宽高位置等需在ready后获取最为准确）
			//设置对象
			let that = this;

		},
		onShow() {						//页面显示后操作（关闭、后台运行等操作后重新打开访问触发、可在实时性较高操作页面进行全页刷新）
			//设置对象
			let that = this;
			//判断是否准备就绪
			if (that.ready) {
				//初始数据
				that.initInformation();
			}
		},
		onLoad(options) {				//页面加载时操作（options可获取当前页面传递参数，如：options.id）
			//设置对象
			let that = this;
			//设置当前页面数据
			that.current_page = that.nii_common.pages.getCurrentUrl();
			//设置默认数据
			that.globalData = that.nii_common.pages.initGlobalData(getApp().globalData);
			//设置参数
			that.options = options;
			that.request_url = "user/" + options.alias + "_log";
			console.log(that.options);
			console.log(that.request_url);
			//初始数据
			that.initInformation();
		},
		methods: {
			//默认跳转操作
			navigateAction: function(alias, requestParams = {}, action = 'navigate', authenticate = false, animationType = 'slide-in-right') {this.nii_common.navigate.action(alias, requestParams, action, authenticate, animationType); },
			//拨打电话
			callPhone: function(mobile) {uni.makePhoneCall({phoneNumber: mobile});},
			//复制数据
			clipboardData: function (text) {const that = this;that.nii_common.clipboardData(text);},
			initInformation: function() {
				//设置对象
				let that = this;
				//loading
				that.nii_common.toastLoading('加载中');			
				//发起请求
				that.nii_common.request.build('user_score/logs', that.options, 'GET', function (res) {
					//设置准备已就绪
					that.ready = true;
					//设置数据
					that.renders = res.data;
					//设置信息
					that.styles.card = ['background:url('+res.data.background+') no-repeat top center / cover'].join(';');
					// that.data.sections.formData = that.renders.lists.formData;
					//获取列表
					that.getLists(true);
				}, function (res) {
					//提示错误
					that.nii_common.toastAlert(res.msg);
				}, function(res) {
					//隐藏loading
					that.nii_common.toastLoadingHide();
				}, true);	
			},
			getLists: function (refresh) {
				//设置对象
				let that = this;
				//判断是否刷新列表
				if (refresh) {					
					that.data.sections.formData.page = 1;
					that.data.sections.lists = {mode: 'refresh', lists: []};	
				}
				//判断是否无追加内容
				if (that.data.sections.lists.mode !== 'finished' && that.data.sections.lists.mode !== 'empty' && that.data.sections.lists.mode !== 'loading') {				
					//设置刷新
					that.data.sections.lists.mode = 'loading';				
					//发起请求
					that.nii_common.request.build('user_score/logs', that.data.sections.formData, 'POST', function (res) {
						//设置已激活
						that.data.sections.lists.mode = 'active';
						//判断是否无数据
						if (uni.$u.test.isEmpty(res.data.lists)) {
							//判断当前页码是否为第一页
							if (parseInt(that.data.sections.formData.page) === 1) {
								//设置数据为空
								that.data.sections.lists.mode = 'empty';
							} else {
								//设置数据已加载完毕
								that.data.sections.lists.mode = 'finished';
							}
						} else {
							//循环列表
							res.data.lists.forEach((good, index) => {
								//增加信息
								that.data.sections.lists.lists.push(good);
							});
							console.log(that.data.sections.lists.lists);
							//判断是否为最后一页
							if (parseInt(res.data.lists.length) < parseInt(res.data.page_size)) {
								//设置数据已加载完毕
								that.data.sections.lists.mode = 'finished';
							}
						}
						//增加页码
						that.data.sections.formData.page += 1;
					}, function (res) {
						//提示错误
						that.nii_common.toastAlert(res.msg);
					}, function(res) {
						//隐藏loading
						that.nii_common.toastLoadingHide();
					}, true);	
				}
			},
			// requestDemo: function() {
			// 	//获取对象
			// 	const that = this;
			// 	//loading
			// 	that.nii_common.toastLoading('处理中...');
			// 	//发起请求
			// 	that.nii_common.request.build('', {}, 'POST', function (res) {
			// 		//提示成功
			// 		that.nii_common.toastAlert('处理成功', 'success');
			// 	}, function (res) {
			// 		//提示错误
			// 		that.nii_common.toastAlert(res.msg);
			// 	}, function(res) {
			// 		//隐藏loading
			// 		that.nii_common.toastLoadingHide();
			// 	});	
			// }
		},
		onPullDownRefresh(res) {
			//设置对象
			let that = this;
			
			//TODO：页面下拉刷新时的事件
			
			
			//停止刷新
			uni.stopPullDownRefresh();	
		},
		onReachBottom() {
			//设置对象
			let that = this;
			
			//TODO：页面滑动触底时的事件
			
			//获取列表
			that.getLists();
		},
		onPageScroll: function(e) {
			//设置页面滑动高度
			this.scroll = e.scrollTop

			//TODO：需判断滑动高度后处理的事件

		}
	}

</script>

<style lang="scss">
	#header_wrapper {
		#title {
			font-size: 40rpx;
			line-height: 48rpx;
			letter-spacing: 1rpx;
			margin-left: 10rpx;
			font-weight: 600;
		}
		.domxee_icon {			
			font-size: 36rpx;
			line-height: 60rpx;
			margin-right: 20rpx;
		}
		.domxee_icon:last-child {
			margin-right: 0;
		}
	}
	#content_wrapper {
		padding-bottom: 300rpx;
		#card {
			margin-top: 40rpx;
			.card {
				.card_box {				
					color: #FFFFFF;
					padding: 70rpx 0 40rpx 30rpx;
					border-radius: 20rpx;
					.titles {
						.title {
							font-size: 26rpx;
							line-height: 36rpx;
						}
						.safe {
							width: max-content;
							background: rgba(255,255,255,0.7);
							border-radius: 30rpx 0rpx 0rpx 30rpx;
							padding: 8rpx 20rpx;
							.img_box {
								width: 40rpx;
								height: 40rpx;
								margin-right: 10rpx;
							}
							.text {
								font-size: 24rpx;
								color: #265FF5;
								line-height: 34rpx;
							}
						}
					}
					.amount {
						margin-top: 10rpx;
						font-size: 60rpx;
						line-height: 84rpx;
					}
					.tip {
						margin-top: 70rpx;
						font-size: 26rpx;
						line-height: 36rpx;
					}
				}
			}
		}
		#lists {
			margin-top: 30rpx;
			.lists {
				.lists_box {
					.title {
						font-size: 32rpx;
						line-height: 44rpx;
					}
					.contents {
						margin-top: 40rpx;
						.content {
							margin-top: 50rpx;
							.left {
								margin-right: 20rpx;
								.img_box {
									width: 60rpx;
									height: 60rpx;
									margin-right: 20rpx;
								}
								.infos {
									.content {
										font-size: 28rpx;
										line-height: 40rpx;
									}
									.time {
										margin-top: 10rpx;
										font-size: 24rpx;
										line-height: 34rpx;
									}
								}
							}
							.right {
								text-align: right;
								.amount {
									font-size: 28rpx;
									line-height: 40rpx;
								}
								.tax {
									margin-top: 10rpx;
									font-size: 24rpx;
									line-height: 34rpx;
								}
							}
						}
						.content:first-child {
							margin-top: 0;
						}
						.lists_loading {
							margin-top: 30rpx;
						}
						.lists_finished {
							margin-top: 30rpx;
							font-size: 26rpx;
							line-height: 34rpx;
						}
						.lists_empty {
							margin-top: 30rpx;
							.domxee_icon {			
								font-size: 300rpx;
								line-height: 300rpx;
							}
							.text {
								margin-top: 10rpx;
								font-size: 26rpx;
								line-height: 34rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
